<template>
  <basic-container>
    <avue-crud :option="option"
               :search.sync="search"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :page.sync="page"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad"

    >
      <template slot="menuLeft">
        <el-button id="id-business-components-offershareuseradd-handleClearSelection"
                   type="success"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   :disabled="selectionList.length == 0"
                   @click="handleClearSelection">清空选择
        </el-button>
        已选用户：{{selectionUserNameList}}
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {getOfferUserPage as getUserList} from "@/api/system/user";
import { addOfferUsers } from "@/api/business/share";
import Moment from 'moment'


export default {
  name:'offerShareUserAdd',
  data() {
    return {
      search: {},
      loading: true,
      form: {},
      query: {},
      data: [],
      selectionList: [],

      option: {
        calcHeight: 80,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        dialogClickModal: false,
        reserveSelection: true,
        rowKey: 'id',
        column: [
          {
            label: "登录账号",
            prop: "account",
            search: true,
            searchSpan:8,
            display: false,
          },

          {
            label: "用户姓名",
            prop: "realName",
            search: true,
            searchSpan:8,
            display: false
          },
          {
            label: "所属角色",
            prop: "roleName",
            slot: true,
            display: false
          },
        ],
        addBtn: false,
        refreshBtn: false,
        columnBtn: false,
        menu: false
      },
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
    }
  },

  computed: {
    selectionUserNameList() {
      let list = [];
      this.selectionList.forEach(row => {
        list.push(row.realName);
      })
      if (list.length == 0) {
        list.push('无');
      }
      return list.join(', ');
    },
  },
  methods: {
    onLoad(page, params = {}) {
      this.loading = true;
      getUserList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query),
        this.$parent.$parent.offerId
      ).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    searchReset() {
      this.query = {};
      this.treeDeptId = '';
      this.onLoad(this.page);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    handleClearSelection() {
      this.$refs.crud.selectClear();
    },
    //提交 user新增
    submitAdd(){
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return false;
      }
      let list = []
      this.selectionList.map(item =>{
        let listItem = {
          offerId : this.$parent.$parent.offerId,
          permission : 1,
          roleName : item.roleName,
          status : item.status,
          userId : item.id,
          userName :item.name,
          createTime: Moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          updateTime: Moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
        }
        list.push(listItem)
      })
      addOfferUsers(list).then( res =>{
        if(res.data.code === 200){
          this.$message({type:'success',message:'新增用户成功!'})
          this.selectionList = [];
          this.$refs.crud.toggleSelection();
          this.$parent.$parent.userAddDialog = false
          this.$parent.$parent.onloadUser(this.$parent.$parent.pageUser)
        }
      })
      return true;
    }
  }
}

</script>








